/*
package controller.testAliSign;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayConstants;
import com.alipay.api.internal.util.StreamUtil;
import com.alipay.api.internal.util.codec.Base64;
import org.apache.commons.lang.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.*;

*/
/**
 * Created by PCG on 2016/9/30.
 *//*

public class TestNotifySign {


    public static void main(String[] args) throws Exception{
        Map<String,String> params = new HashMap<String, String>();
        params.put("app_id","2016092301959945");
        params.put("buyer_id","2088602278594322");
        params.put("gmt_create","2016-09-30 15:16:05");
        params.put("gmt_payment","2016-09-30 15:16:06");
        params.put("notify_id","4819f4c95214cad65b4511b9c4646fdigy");
        params.put("notify_time","2016-09-30 15:16:07");
        params.put("notify_type","trade_status_sync");
        params.put("out_trade_no","0525227326160236402");
        params.put("seller_id","2088421881869056");
        params.put("subject","神州专车产品");
        params.put("total_amount","0.01");
        params.put("trade_no","2016093021001004320249583798");
        params.put("trade_status","TRADE_SUCCESS");
        params.put("sign_type","RSA");
        params.put("sign","v6AajKedgcmFORcr8nOqvRrmgYUbes+Z8c9PnJbqYaZj7xqM7hs1g3S5L2HOmGy5m7vQDCMoP9u+kl+GVoVsrzuJ8QwTNvDOuX93TLy4QGp+7vHqza+CHWkBqFSqzl/NMr62yemsOM9ghihQ1FY6hfrI+xnOqNXaFbxvd74kKv0=");

        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDI6d306Q8fIfCOaTXyiUeJHkrIvYISRcc73s3vF1ZT7XN8RNPwJxo8pWaJMmvyTn9N4HQ632qJBVHf8sxHi/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB";
        System.out.println(rsaCheckV1(params, publicKey, "utf-8"));

    }

    public static boolean rsaCheckV1(Map<String, String> params, String publicKey,
                                     String charset) throws AlipayApiException {
        String sign = params.get("sign");
        String content = getSignCheckContentV1(params);

        return rsaCheckContent(content, sign, publicKey, charset);
    }


    public static String getSignCheckContentV1(Map<String, String> params) {
        if (params == null) {
            return null;
        }

        params.remove("sign");
        params.remove("sign_type");

        StringBuffer content = new StringBuffer();
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);

        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            content.append((i == 0 ? "" : "&") + key + "=" + value);
        }

        return content.toString();
    }

    public static boolean rsaCheckContent(String content, String sign, String publicKey,
                                          String charset) throws AlipayApiException {
        try {
            PublicKey pubKey = getPublicKeyFromX509("RSA",
                    new ByteArrayInputStream(publicKey.getBytes()));

            java.security.Signature signature = java.security.Signature
                    .getInstance(AlipayConstants.SIGN_ALGORITHMS);

            signature.initVerify(pubKey);

            if (StringUtils.isEmpty(charset)) {
                signature.update(content.getBytes());
            } else {
                signature.update(content.getBytes(charset));
            }

            return signature.verify(Base64.decodeBase64(sign.getBytes()));
        } catch (Exception e) {
            throw new AlipayApiException(
                    "RSAcontent = " + content + ",sign=" + sign + ",charset = " + charset, e);
        }
    }

    public static PublicKey getPublicKeyFromX509(String algorithm,
                                                 InputStream ins) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance(algorithm);

        StringWriter writer = new StringWriter();
        StreamUtil.io(new InputStreamReader(ins), writer);

        byte[] encodedKey = writer.toString().getBytes();

        encodedKey = Base64.decodeBase64(encodedKey);

        return keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
    }

}
*/
